// XTPPropertyGridItemSize.cpp : implementation of the CXTPPropertyGridItemSize class.
//
// This file is a part of the XTREME PROPERTYGRID MFC class library.
// (c)1998-2012 Codejock Software, All Rights Reserved.
//
// THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
// RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
// CONSENT OF CODEJOCK SOFTWARE.
//
// THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
// IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
// YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
// SINGLE COMPUTER.
//
// CONTACT INFORMATION:
// support@codejock.com
// http://www.codejock.com
//
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "XTPPropertyGridItemGridPos.h"
#include "XTPPropertyGridItemShort.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CXTPPropertyGridItemGridPos::CItemX

class CXTPPropertyGridItemGridPos::CItemX : public CXTPPropertyGridItemShort
{
public:
	CItemX(LPCTSTR strCaption)
		: CXTPPropertyGridItemShort(strCaption)
	{
	}

	virtual void SetValue(CString strValue)
	{
		CXTPPropertyGridItemShort::SetValue(strValue);
		OnValueChanged(strValue);
	}
	virtual void OnValueChanged(CString strValue)
	{
		((CXTPPropertyGridItemGridPos*)m_pParent)->SetX(strValue);
	}
	virtual BOOL GetReadOnly() const
	{
		return m_pParent->GetReadOnly();
	}
};

/////////////////////////////////////////////////////////////////////////////
// CXTPPropertyGridItemGridPos::CItemY

class CXTPPropertyGridItemGridPos::CItemY : public CXTPPropertyGridItemShort
{
public:
	CItemY(LPCTSTR strCaption)
		: CXTPPropertyGridItemShort(strCaption)
	{
	}

	virtual void SetValue(CString strValue)
	{
		CXTPPropertyGridItemShort::SetValue(strValue);
		OnValueChanged(strValue);
	}
	virtual void OnValueChanged(CString strValue)
	{
		((CXTPPropertyGridItemGridPos*)m_pParent)->SetY(strValue);
	}
	virtual BOOL GetReadOnly() const
	{
		return m_pParent->GetReadOnly();
	}
};

/////////////////////////////////////////////////////////////////////////////
// CXTPPropertyGridItemGridPos
IMPLEMENT_DYNAMIC(CXTPPropertyGridItemGridPos, CXTPPropertyGridItem)

CXTPPropertyGridItemGridPos::CXTPPropertyGridItemGridPos(LPCTSTR strCaption, CGridPos pt, CGridPos* pBindPoint)
	: CXTPPropertyGridItem(strCaption)
{
	m_pItemX = NULL;
	m_pItemY = NULL;
	m_ptValue = pt;
	BindToPoint(pBindPoint);
	m_strDefaultValue = m_strValue = PointToString(pt);
}

CXTPPropertyGridItemGridPos::CXTPPropertyGridItemGridPos(UINT nID, CGridPos pt, CGridPos* pBindPoint)
	: CXTPPropertyGridItem(nID)
{
	m_pItemX = NULL;
	m_pItemY = NULL;
	m_ptValue = pt;
	BindToPoint(pBindPoint);
	m_strDefaultValue = m_strValue = PointToString(pt);
}

CXTPPropertyGridItemGridPos::~CXTPPropertyGridItemGridPos()
{

}

/////////////////////////////////////////////////////////////////////////////
//

void CXTPPropertyGridItemGridPos::OnAddChildItem()
{
	if (m_pItemX && m_pItemY)
		return;

	m_pItemX = (CItemX*)AddChildItem(new CItemX(_T("X")));
	m_pItemY = (CItemY*)AddChildItem(new CItemY(_T("Y")));

	UpdateChilds();

	m_pItemX->SetDefaultValue(m_pItemX->GetValue());
	m_pItemY->SetDefaultValue(m_pItemY->GetValue());
}

CString CXTPPropertyGridItemGridPos::PointToString(CGridPos pt)
{
	CString str;
	str.Format(_T("%i; %i"), pt.pos.x, pt.pos.y);

	return str;
}

CGridPos CXTPPropertyGridItemGridPos::StringToPoint(LPCTSTR str)
{
	int x = NextNumber(str);
	int y = NextNumber(str);

	return CGridPos(x, y);
}

void CXTPPropertyGridItemGridPos::SetValue(CString strValue)
{
	SetPoint(StringToPoint(strValue));
}

void CXTPPropertyGridItemGridPos::SetPoint(CGridPos pt)
{
	m_ptValue = pt;

	if (m_pBindPoint)
	{
		*m_pBindPoint = m_ptValue;
	}

	CXTPPropertyGridItem::SetValue(PointToString(m_ptValue));
	UpdateChilds();
}

void CXTPPropertyGridItemGridPos::BindToPoint(CGridPos* pBindPoint)
{
	m_pBindPoint = pBindPoint;
	if (m_pBindPoint)
	{
		*m_pBindPoint = m_ptValue;
	}
}

void CXTPPropertyGridItemGridPos::OnBeforeInsert()
{
	if (m_pBindPoint && (m_pBindPoint->pos.x != m_ptValue.pos.x || m_pBindPoint->pos.y != m_ptValue.pos.y))
	{
		SetPoint(*m_pBindPoint);
	}
}

void CXTPPropertyGridItemGridPos::UpdateChilds()
{
	m_pItemX->SetNumber(m_ptValue.pos.x);
	m_pItemY->SetNumber(m_ptValue.pos.y);
}

void CXTPPropertyGridItemGridPos::SetX(LPCTSTR sx)
{
	OnValueChanged(PointToString(CGridPos(_ttoi(sx), m_ptValue.pos.y)));
}

void CXTPPropertyGridItemGridPos::SetY(LPCTSTR sy)
{
	OnValueChanged(PointToString(CGridPos(m_ptValue.pos.x, _ttoi(sy))));
}
